<?php

namespace Admin\Model;

use Think\Model;

/**
* 
*/
class CourseModel extends Model
{
	/**
	 * 课程列表
	 * @param  string $passed 是否审核通过 0待审核，1通过，2未通过
	 * @return [type]         [description]
	 * @author qlj
	 */
	public function courselist($passed = ''){

		$map['passed'] = $passed;
		$map['status'] = 1;
		//课程类型，1学，2教
		$map['teaching'] = 2;

		//分页
		$count = $this -> where($map) -> count();
		$page = new \Think\Page($count, 10);
		$page -> setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
		$pages = $page -> show();

		$where['c.passed'] = $passed;
		$where['c.status'] = 1;
		//课程类型，1学，2教
		// $where['c.teaching'] = 2;
		$data = $this -> field('c.*,u.nickname') -> alias('c') -> join('tutor_user_member as u on c.uid=u.id') -> where($where) -> order('c.create_time desc') -> limit($page->firstRow, $page->listRows) -> select();
		return array($data,$pages);
	}

	/**
	 * 修改
	 * @param  [int] $id   [主键]
	 * @param  [int] $val  [值]
	 * @param  [str] $flag [字段]
	 * @return [array]     [更新的行数]
	 * @author qlj
	 */
	public function edit($id,$val,$flag){
		return $this -> where(array('id' => $id)) -> save(array($flag => $val));
	}

	/**
	 * 通过课程id查找用户评价
	 * @param  [int] $id [课程id]
	 * @return [int]     [uid]
	 * @author qlj
	 */
	public function courseUid($id){
		return $this -> alias('c') -> field('c.id,e.uid') -> join('tutor_teacher_eva as e on c.uid=e.uid') -> where(array('c.id' => $id)) -> find();
	}
}